package com.njitt.bysj.srevice;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njitt.bysj.base.BaseService;
import com.njitt.bysj.bean.InventoryBean;
import com.njitt.bysj.bean.PurchaseRequestBean;
import com.njitt.bysj.bean.WarehouseBean;
import com.njitt.bysj.controller.WareHouseController;
import com.njitt.bysj.mapper.InventoryBeanMapper;
import com.njitt.bysj.mapper.PurchaseRequestBeanMapper;
import com.njitt.bysj.mapper.WarehouseBeanMapper;
import com.njitt.bysj.model.WarehouseModel;
import com.njitt.bysj.query.WarehouseQuery;
import com.njitt.bysj.utils.AssertUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: wyj
 * @time: 2022/4/18 12:40
 */
@Service
public class WarehouseBeanService extends BaseService<WarehouseBean, Integer> {
    @Resource
    private WarehouseBeanMapper warehouseBeanMapper;

    @Resource
    private PurchaseRequestBeanMapper purchaseRequestBeanMapper;

    @Resource
    private InventoryBeanMapper inventoryBeanMapper;

    public Map<String, Object> selectByParamsList(WarehouseQuery query) {
        PageHelper.startPage(query.getPage(), query.getLimit());
        PageInfo<WarehouseBean> info = new PageInfo<>(warehouseBeanMapper.selectByParams(query));
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("data", info.getList());
        map.put("msg", "success");
        map.put("count", info.getTotal());
        return map;
    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void purchaseToWareHouse(Integer purchaseId) {
        /**
         * @description: 库存入库
         * @return:  * @param purchaseId
         * @author:
         * @time: 2022/4/20 10:21
         */

        PurchaseRequestBean bean = purchaseRequestBeanMapper.selectByPrimaryKey(purchaseId);
        WarehouseQuery warehouseQuery = new WarehouseQuery();
        List<WarehouseBean> warehouseBeans = warehouseBeanMapper.selectByParams(warehouseQuery);
        warehouseBeans.forEach(w -> {
            if (bean.getMaterialNo().equals(w.getMaterialNo())) {
                Integer count = w.getMaterialCount() + bean.getPurchaseCount();
                WarehouseModel model = new WarehouseModel();
                model.setId(w.getId());
                model.setMaterialNo(w.getMaterialNo());
                model.setMaterialCount(count);
                Integer integer = warehouseBeanMapper.updateWareWithInto(model);
                AssertUtil.isTrue(integer != 1, "入库失败");
            }
        });
    }

    public Map<String, Object> selectProduceByParams(WarehouseQuery query) {
        /**
         * @description:
         * @return:  * @param query
         * @author: 产成品库存
         * @time: 2022/4/24 17:42
         */
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        PageInfo<InventoryBean> info = new PageInfo<>(inventoryBeanMapper.selectByParams(query));
        map.put("code", 0);
        map.put("data", info.getList());
        map.put("msg", "success");
        map.put("count", info.getTotal());
        return map;
    }
    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void outProduce(Integer id) {
        AssertUtil.isTrue(id == null || id == 0, "审核失败");
        InventoryBean inventoryBean = inventoryBeanMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(inventoryBean == null, "深刻失败 库存不足");
        inventoryBean.setOutStatus(2);
        inventoryBean.setStockCount(inventoryBean.getProjectBean().getBasicQuotationBean().getProductCount().intValue() - inventoryBean.getOutCount());
        Integer integer = inventoryBeanMapper.updateByPrimaryKeySelective(inventoryBean);
        AssertUtil.isTrue(integer == null || integer == 0, "发料失败");
    }
}
